package com.yfp.machine.enums;

import cn.hutool.core.date.DateUtil;
import com.yfp.common.core.exception.ServiceException;

import java.util.Date;
import java.util.Objects;

/**
 * 播放状态枚举
 *
 *
 * @date 2023/12/28
 */
public enum PalyStatusEnum {

    //1：未开始 2：播放中 3：已结束
    UNSTART(1, "待投放"),
    PLAYING(2, "投放中"),
    END(3, "已结束");

    private Integer code;
    private String desc;

    PalyStatusEnum(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public static String getDescByCode(Integer code) {
        for (PalyStatusEnum palyStatusEnum : PalyStatusEnum.values()) {
            if (palyStatusEnum.getCode().equals(code)) {
                return palyStatusEnum.getDesc();
            }
        }
        throw new ServiceException("未找到对应的枚举值");
    }


    public static PalyStatusEnum getPlayStatusEnum(Date playBeginTime, Date playEndTime) {
        if (Objects.isNull(playBeginTime) || Objects.isNull(playEndTime)){
            throw new ServiceException("枚举类型转换类型入参不能为空");
        }
        Date now = new Date();
        if (playBeginTime.after(now)){
            return PalyStatusEnum.UNSTART;
        }else if (DateUtil.isIn(now, playBeginTime, playEndTime)){
            return PalyStatusEnum.PLAYING;
        }else if (playEndTime.before(now)){
            return PalyStatusEnum.END;
        }else {
            throw new ServiceException("解析时间失败");
        }
    }

}
